<template>
    <orh-table reload  @on-reload="reload" add="add" @on-add="moralAdd" del="del" @on-del="delConfirm" pages :pageDate="pageDate" @on-page-change="reload">
        
        <Table border stripe :columns="columns" @on-selection-change="selectOne" :data="data" :loading="tableload" slot="table"></Table>

        <orh-sidebar v-model="show" :title="titles" scale slot="others">
            <component :is="content" :condition="condition" @requery="requery"></component>
        </orh-sidebar>

    </orh-table>
</template>
                
<script>

export default {
    name: 'dutymodule-specialitydata',
    components: {
        Operation: resolve => {require(['./Operation.vue'], resolve);},
        Confirm: resolve => {require(['./Confirm.vue'], resolve);}
    },
    data () {
        return {
            show: false,
            content: null,
            titles: null,
            tableload: true,
            pageDate: {
                total: 0,
                current_page: 1,
                per_page: 0
            },
            data:[],
            columns:[
                {
                    type: 'selection',
                    width: 60,
                    align: 'center',
                    fixed:'left'
                },
                {
                    title: '类型',
                    align: 'center',
                    minWidth: 150,
                    fixed:'left',
                    render: (h, params) => {
                        let name = params.row.type_name?params.row.type_name.zh?params.row.type_name.zh:params.row.type_name.mn:''
                        return h('div', name)
                    }
                },
                {
                    title: '学生',
                    minWidth: 150,
                    align: 'center',
                    render: (h, params) => {
                        let datas = params.row.student
                        let label = []
                        label = datas.map((el, i) => {
                            return el.student_name.zh != null?i == 0?el.student_name.zh:` / ${el.student_name.zh}`:''
                        })
                        return h('div', label);
                    }
                },
                {
                    title: '添加人',
                    minWidth: 150,
                    align: 'center',
                    render: (h, params) => {
                        let label = params.row.created_by[0].teacher_name.zh
                        return h('div', label)
                    }
                },
                {
                    title: '添加时间',
                    minWidth: 180,
                    align: 'center',
                    key: 'created_at',
                },
                {
                    title: '确认老师',
                    minWidth: 150,
                    align: 'center',
                    render: (h, params) => {
                        let state = params.row.updated_by
                        let label = ''
                        let color = ''
                        if(state != null) { 
                            label = state[0].teacher_name.zh
                            color = '#19be6b'
                        }else{ 
                            label = '未确认'
                            color = '#f16543'
                        }
                        return h('div', {
                            style:{
                                color: color
                            }
                        }, label);
                    }
                },
                {
                    title: '细则',
                    minWidth: 150,
                    align: 'center',
                    render: (h, params) => {
                        let label = params.row.rules?params.row.rules.entry_content.zh:''
                        return h('div', label);
                    }
                },
                {
                    title: '细则类型',
                    minWidth: 150,
                    align: 'center',
                    render: (h, params) => {
                        let state = params.row.rules?params.row.rules.classification:''
                        let label = ''
                        let color = ''
                        if (state == 1) { 
                            label = '加分' 
                            color = '#19be6b'
                        } else if (state == 2) { 
                            label = '减分'
                            color = '#f16543'
                        } else {
                            label = ''
                            color = '#f16543'
                        }
                        return h('div', {
                            style:{
                                color: color
                            }
                        }, label);
                    }
                },
                {
                    title: '备注',
                    minWidth: 100,
                    align: 'center',
                    render:(h,params)=> {
                        return h('span',params.row.remarks?params.row.remarks.zh?params.row.remarks.zh:params.row.remarks.mn:'')
                    },
                },
                {
                    title: '操作',
                    key: 'action',
                    minWidth: 200,
                    align: 'center',
                    fixed:'right',
                    render: (h, params) => {
                        let state = this.data[params.index].confirm
                        let states = false
                        let names = ''
                        if(state == '1'){
                            states = false
                            names = '未确认'
                        }else{
                            states = true
                            names = '已确认'
                        }
                        return h('div', [
                            h('Button', {
                                directives: [
                                    {
                                        name: 'button',
                                        value: 'confirm',
                                    }
                                ],
                                props: {
                                    type: 'info',
                                    size: 'small',
                                },
                                style: {
                                    marginRight: '5px',
                                },
                                on: {
                                    click: () => {
                                        this.ConfirmData(params.row.data_log_id)
                                    }
                                }
                            }, names),
                            h('Button', {
                                directives: [
                                    {
                                        name: 'button',
                                        value: 'edit',
                                    }
                                ],
                                props: {
                                    type: 'primary',
                                    size: 'small',
                                    disabled: states
                                },
                                style: {
                                    marginRight: '5px',
                                },
                                on: {
                                    click: () => {
                                        this.updata(params.row.data_log_id)
                                    }
                                }
                            }, '修改'),
                            h('Poptip', {
                                props: {
                                    confirm: true,
                                    transfer: true,
                                    title: '您确定要删除这条数据吗?',
                                    type: 'error',
                                    size: 'small',
                                    placement:'left-end'
                                },
                                style: {
                                    textAlign: 'left',
                                },
                                on: {
                                    'on-ok': () => {
                                        this.remove(params.row.data_log_id)
                                    }
                                }
                            }, [
                                h('Button', {
                                    directives: [
                                        {
                                            name: 'button',
                                            value: 'del',
                                        }
                                    ],
                                    props: {
                                        type: 'error',
                                        size: 'small',
                                        disabled: states
                                    }
                                }, '删除')
                            ]),
                        ]);
                    }
                },
            ],
            url: 'api/watchlist/v1/phoneticbeautytdata/list',
            delUrl: 'api/watchlist/v1/phoneticbeautytdata/del/',
            del: [],
            condition: null,
        }
    },

    methods: {
        
        /************************************************************
         * 
         * 删除确认
         * 
         ************************************************************/
        delConfirm () {
            if (this.del.length) {
                this.$Modal.confirm({
                    title: '删除确认',
                    closable: true,
                    loading: true,
                    content: '您确定要删除这些数据吗?',
                    onOk: () => {
                        this.delSubmit ()
                    }
                })
            }
        },
        
        /************************************************************
         * 
         * 添加数据
         * 
         ************************************************************/
        moralAdd () {
            this.condition = false
            this.titles = '添加数据'
            this.show = true
            this.content = 'Operation'
        },
        
        /************************************************************
         * 
         * 单条数据删除
         * @param   {index}            当前数据id
         * 
         ************************************************************/
        remove (index) {
            this.del = []
            this.del.push(index)
            this.delSubmit()
        },
        
        /************************************************************
         * 
         * 修改数据
         * @param   {index}            当前数据id
         * 
         ************************************************************/
        updata (index) {
            this.condition = index
            this.titles = '修改数据'
            this.show = true
            this.content = 'Operation'
        },
        
        /************************************************************
         * 
         * 确认数据
         * @param   {index}            当前数据id
         * 
         ************************************************************/
        ConfirmData (index) {
            this.condition = index
            this.titles = '确认数据'
            this.show = true
            this.content = 'Confirm'
        },
        
        /************************************************************
         * 
         * 批量删除数据
         * @param   {index}            所选数据id[array]
         * 
         ************************************************************/
        selectOne (index) {
            this.del = []
            index.forEach(item => {
                this.del.push(item.data_log_id)
            });
        },
        
        /************************************************************
         * 
         * 删除操作
         * 
         ************************************************************/
        delSubmit () {
            if(this.del.length == 0){
                this.$Message.info('请选择删除项')
                this.prompt = false
                return
            }
            this.del = JSON.stringify(this.del)
            this.$http.delete(this.delUrl + this.del).then(val=>{
                this.$Modal.remove ()
                this.reload (this.pageDate.current_page)
            }).catch(err => {
                this.$Modal.remove ()
            })
        },
        
        /************************************************************
         * 
         * 添加，修改数据成功后关闭组件并刷新数据
         * @param   {state}            状态
         * 
         ************************************************************/
        requery (state) {
            if(state){
                this.show = false
                this.reload (this.pageDate.current_page)
            }
        },
        
        /************************************************************
         * 
         * 刷新数据
         * @param   {index}            分页
         * 
         ************************************************************/
        reload (index) {
            this.data = []
            this.tableload = true
            this.queryData(index?index:1)
        },
        
        /************************************************************
         * 
         * 列表数据
         * @param   {index}             分页
         * 
         ************************************************************/
        queryData (index) {
            let params = {
                page: index
            }
            this.$http.get(this.url, { params }).then(val=>{
                this.pageDate.per_page = val.pages.per_page
                this.pageDate.total = val.pages.total
                this.pageDate.current_page = val.pages.current_page
                this.data = val.data 
                this.tableload = false
            }).catch(err => {
                this.tableload = false
            })
        },
    },
    created () {
        this.reload (1)
    },
}
</script>